/**
 * Created by IntelliJ IDEA.
 * User: TBear
 * Date: 2010-6-8
 * Time: 13:09:55
 * To change this template use File | Settings | File Templates.
 */

class SecurityFilters {
  def filters = {
    loginCheck(controller: '*', action: '*') {
      before = {
        boolean bNeedAdminFlag = true;
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");

        def action = actionName?actionName:"index";
        if(!controllerName){
          return true;
        }
        Resource resource = Resource.findByControllerNameAndActionName(controllerName,action);
        if(resource){
          if(!session.currentUser){                                           //未登录
            redirect(controller: "loginSystem", action: 'login');
            return false;
          }else{
            for(Authority authority : UserInfo.get(session.currentUser.id).authorities){
                  for(Resource resourceInstan :authority.resources ){
                     if(resourceInstan.id == resource.id)
                          return true;
                  }
            }
          }
          redirect(controller: "loginSystem", action: 'popedom');  //无权限
          return false;
        }

        return true;
      }
    }
  }
}